﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace User.Api.Common
{

    /// <summary>
    /// 统一返回结果
    /// </summary>
    public class ResultData
    {
        //: IHttpActionResult
        /// <summary>
        /// 错误信息
        /// </summary>
        public string message { set; get; }

        /// <summary>
        /// 返回状态
        /// 0：成功,否则失败
        /// </summary>
        public int status { set; get; }

        /// <summary>
        /// 返回信息
        /// </summary>
        public object data { set; get; }
        public static ResultData Success()
        {
            ResultData result = new ResultData
            {
                status = 0,
                message = "",
            };
            return result;
        }
        public static ResultData Success(object _data)
        {
            ResultData result = new ResultData
            {
                status = 0,
                message = string.Empty,
                data = _data
            };
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static ResultData Success(string msg)
        {
            ResultData result = new ResultData
            {
                status = 0,
                message = msg,
            };
            return result;
        }
        public static ResultData Error()
        {
            ResultData result = new ResultData
            {
                status = -1,
                message = "未知错误"
            };
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        public static ResultData Error(string errMsg)
        {
            ResultData result = new ResultData
            {
                status = -1,
                message = errMsg
            };
            return result;
        }
    }
    /// <summary>
    /// Api统一返回结果模板类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ResultData<T> 
    {
        /// <summary>
        /// 构造
        /// </summary>
        public ResultData()
        {
            message = "ok";
            status = 0;
        }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string message { set; get; }

        /// <summary>
        /// 返回状态
        /// 0：成功,否则失败
        /// </summary>
        public int status { set; get; }

        /// <summary>
        /// 返回信息
        /// </summary>
        public T data { set; get; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_data"></param>
        /// <returns></returns>
        public static ResultData<T> Success(T _data)
        {
            ResultData<T> result = new ResultData<T>
            {
                status =0,
                message = string.Empty,
                data = _data
            };
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static ResultData<T> Success(string msg)
        {
            ResultData<T> result = new ResultData<T>
            {
                status = 0,
                message = msg,
            };
            return result;
        }
        public static ResultData<T> Error()
        {
            ResultData<T> result = new ResultData<T>
            {
                status = -1,
                message = "未知错误"
            };
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        public static ResultData<T> Error(string errMsg)
        {
            ResultData<T> result = new ResultData<T>
            {
                status = -1,
                message = errMsg
            };
            return result;
        }

        //public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        //{
        //    var formatter = new JsonMediaTypeFormatter();
        //    //返回时间类型数据不带T
        //    formatter.SerializerSettings.Converters.Add(
        //        new Newtonsoft.Json.Converters.IsoDateTimeConverter()
        //        {
        //            DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
        //        }
        //        );
        //    var response = new HttpResponseMessage()
        //    {
        //        Content = new ObjectContent(typeof(object), this, formatter),
        //    };
        //    return Task.FromResult(response);
        //}
    }
}